﻿using MediatR;
using Microsoft.AspNetCore.SignalR;
using System.Collections.Concurrent;

namespace B.Z.Inventory.API.Read
{
    public class InventoryReadHub:Hub
    {
        private readonly IMediator mediator;

        public InventoryReadHub(IMediator mediator)
        {
            this.mediator = mediator;
        }

        #region 连接管理

        // 用户连接时
        public override async Task OnConnectedAsync()
        {
            var connectionId = Context.ConnectionId;
            Console.WriteLine($"用户连接: {connectionId}");
            string message = "用户连接成功";
            await Clients.All.SendAsync("OnConnectedAsync",message);
            await base.OnConnectedAsync();
        }

        // 用户断开连接时
        public override async Task OnDisconnectedAsync(Exception exception)
        {
            var connectionId = Context.ConnectionId;

            string str = $"用户断开连接: {connectionId}";
            await Clients.Caller.SendAsync("OnDisconnectedAsync", str);
            await base.OnDisconnectedAsync(exception);
        }

        // 加入组
        public async Task JoinGroup(string userName)
        {
            await Groups.AddToGroupAsync(Context.ConnectionId, "LoginMessage");
        }

        #endregion

        #region 库存实时推送方法

        // 推送库存数量占比数据
        public async Task SendInventoryQuantityRatio(object data)
       {
            await Clients.All.SendAsync("ReceiveInventoryQuantityRatio", data);
        }

        // 推送库存金额占比数据
        public async Task SendInventoryAmountRatio(object data)
        {
            await Clients.All.SendAsync("ReceiveInventoryAmountRatio", data);
        }

        // 推送出入库统计数据
        public async Task SendInOutboundStatistics(object data)
        {
            await Clients.All.SendAsync("ReceiveInOutboundStatistics", data);
        }

        // 推送实时库存变更
        public async Task SendInventoryUpdate(object data)
        {
            await Clients.All.SendAsync("ReceiveInventoryUpdate", data);
        }

        // 推送综合仪表板数据
        public async Task SendDashboardData(object data)
        {
            await Clients.All.SendAsync("ReceiveDashboardData", data);
        }

        // 推送出入库排名数据
        public async Task SendInOutboundRanking(object data)
        {
            await Clients.All.SendAsync("ReceiveInOutboundRanking", data);
        }

        // 推送库存预警信息
        public async Task SendLowStockAlert(object data)
        {
            await Clients.All.SendAsync("ReceiveLowStockAlert", data);
        }

        // 推送入库成功消息
        public async Task SendInboundSuccess(object data)
        {
            await Clients.All.SendAsync("ReceiveInboundSuccess", data);
        }

        // 推送出库成功消息
        public async Task SendOutboundSuccess(object data)
        {
            await Clients.All.SendAsync("ReceiveOutboundSuccess", data);
        }

        // 推送系统消息
        public async Task SendSystemMessage(object data)
        {
            await Clients.All.SendAsync("ReceiveSystemMessage", data);
        }

        // 加入监控组（用于特定类型的消息推送）
        public async Task JoinMonitoringGroup(string groupName)
        {
            await Groups.AddToGroupAsync(Context.ConnectionId, groupName);
            await Clients.Caller.SendAsync("JoinedGroup", $"已加入监控组：{groupName}");
        }

        // 离开监控组
        public async Task LeaveMonitoringGroup(string groupName)
        {
            await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName);
            await Clients.Caller.SendAsync("LeftGroup", $"已离开监控组：{groupName}");
        }

        6

        #endregion

    }
}
